package com.lamp.main;

import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.lamp.facebook.BaseRequestListener;
import com.lamp.facebook.LoginButton;
import com.lamp.facebook.SessionEvents;
import com.lamp.facebook.SessionEvents.AuthListener;
import com.lamp.facebook.SessionEvents.LogoutListener;
import com.lamp.facebook.SessionStore;

public class Begin extends Activity {

	public static final String APP_ID = "115579455190819";

	private Facebook mFacebook;
	private AsyncFacebookRunner mAsyncRunner;
	private LoginButton mLoginButton;

	ImageView iconIV;
	Button mn_btn;
	Button en_btn;
	ImageButton register_btn;
	ImageButton login;

	DisplayMetrics o;
	RelativeLayout background1IV;
	ImageView lightIV;
	LinearLayout layout2;
	LinearLayout layout3;
	LinearLayout contentLL;
	TranslateAnimation translateAnim;
	boolean before = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.begin);

		mLoginButton = (LoginButton) findViewById(R.id.login_facebook);
		mFacebook = new Facebook(APP_ID);
		mAsyncRunner = new AsyncFacebookRunner(mFacebook);

		SessionStore.restore(mFacebook, this);
		SessionEvents.addAuthListener(new SampleAuthListener());
		SessionEvents.addLogoutListener(new SampleLogoutListener());
		String[] permissions = { "publish_stream", "user_checkins",
				"friends_checkins", "user_birthday", "user_location", "email" };
		mLoginButton.init(this, mFacebook, permissions);

		background1IV = (RelativeLayout) findViewById(R.id.background1IV);
		lightIV = (ImageView) findViewById(R.id.lightIV);
		layout2 = (LinearLayout) findViewById(R.id.layout2);
		layout3 = (LinearLayout) findViewById(R.id.layout3);
		o = new DisplayMetrics();
		o.density = getBaseContext().getResources().getDisplayMetrics().density;
		o.densityDpi = getBaseContext().getResources().getDisplayMetrics().densityDpi;
		o.heightPixels = getBaseContext().getResources().getDisplayMetrics().heightPixels;
		o.scaledDensity = getBaseContext().getResources().getDisplayMetrics().scaledDensity;
		o.widthPixels = getBaseContext().getResources().getDisplayMetrics().widthPixels;
		o.xdpi = getBaseContext().getResources().getDisplayMetrics().xdpi;
		o.ydpi = getBaseContext().getResources().getDisplayMetrics().ydpi;
		Log.d("Display metric", getBaseContext().getResources()
				.getDisplayMetrics().toString());
		contentLL = (LinearLayout) findViewById(R.id.contentLL);

		iconIV = (ImageView) findViewById(R.id.iconIV);
		Log.v("H W", iconIV.getWidth() + " " + iconIV.getHeight());
		ImageButton begin_btn_1 = (ImageButton) findViewById(R.id.begin_btn_1);
		begin_btn_1.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				int[] a = new int[2];
				iconIV.getLocationInWindow(a);
				Log.v("H W", iconIV.getWidth() + " " + iconIV.getHeight());

				Log.v("Location", a[0] + " " + a[0]);

			}
		});
		mn_btn = (Button) findViewById(R.id.mn_btn);
		en_btn = (Button) findViewById(R.id.en_btn);
		login = (ImageButton) findViewById(R.id.begin_btn_1);
		login.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(Begin.this, LoginActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.alpha, R.anim.alpha2);

			}
		});
		mn_btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				changeMn();
				// mn.setBackgroundResource(R.drawable.eng);
				// en.setBackgroundResource(R.drawable.mngbw);
			}
		});
		en_btn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				changeEn();
				// mn.setBackgroundResource(R.drawable.engbw);
				// en.setBackgroundResource(R.drawable.mng);
			}
		});
		register_btn = (ImageButton) findViewById(R.id.register_btn);
		register_btn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// Intent intent = new Intent();
				// intent.setClass(Begin.this, RegisterActivity.class);
				// startActivity(intent);
				// overridePendingTransition(R.anim.in_from_bottom,
				// R.anim.out_to_top);

			}
		});

		contentLL
				.setAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha));
		background1IV.setAnimation(AnimationUtils.loadAnimation(this,
				R.anim.alpha));
		background1IV.setVisibility(View.VISIBLE);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);

		if (before) {
			int[] a = new int[2];
			iconIV.getLocationInWindow(a);
			Log.v("onWindowFocusChanged",
					iconIV.getWidth() + " " + iconIV.getHeight());
			a[0] += iconIV.getWidth() / 2;
			a[1] += iconIV.getHeight() / 2;
			int[] b = new int[2];
			lightIV.getLocationOnScreen(b);
			b[0] += lightIV.getWidth() / 2;
			b[1] += lightIV.getHeight() / 2;
			Log.v("onWindowFocusChanged", a[0] + " " + a[1] + " " + b[0] + " "
					+ b[1]);

			final Animation alpa = AnimationUtils.loadAnimation(this,
					R.anim.alpha);

			translateAnim = new TranslateAnimation(0f, 0f, 0f, a[1] - b[1]);
			translateAnim.setFillAfter(true);
			translateAnim.setDuration(1000);
			lightIV.startAnimation(translateAnim);
			translateAnim.setAnimationListener(new AnimationListener() {

				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub

				}

				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				public void onAnimationEnd(Animation animation) {
					// TODO Auto-generated method stub\
					background1IV.setAnimation(AnimationUtils.loadAnimation(
							Begin.this, R.anim.alpha1));

					background1IV.setVisibility(View.INVISIBLE);

					contentLL.setAnimation(alpa);
					contentLL.setVisibility(View.VISIBLE);
					alpa.setAnimationListener(new AnimationListener() {

						public void onAnimationStart(Animation animation) {
							// TODO Auto-generated method stub

						}

						public void onAnimationRepeat(Animation animation) {
							// TODO Auto-generated method stub

						}

						public void onAnimationEnd(Animation animation) {
							Animation scale = AnimationUtils.loadAnimation(
									Begin.this, R.anim.alpha);
							layout2.setAnimation(scale);
							layout3.setAnimation(scale);
							layout2.setVisibility(View.VISIBLE);
							layout3.setVisibility(View.VISIBLE);

						}
					});

				}
			});
			before = false;
		}
	}

	public void changeLocalModul() {
		TextView login = (TextView) findViewById(R.id.login_edt);
		TextView register = (TextView) findViewById(R.id.register_edt);
		TextView txtLogin = (TextView) findViewById(R.id.login_with_edt);
		TextView copyright = (TextView) findViewById(R.id.copyright_edt);
		login.setText(R.string.login);
		register.setText(R.string.register);
		txtLogin.setText(R.string.login_with);
		copyright.setText(R.string.begin);
		getBaseContext().getResources().getDisplayMetrics().setTo(o);

	}

	public void changeMn() {
		String languageToLoad = "mn";
		Locale locale = new Locale(languageToLoad);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config,
				getBaseContext().getResources().getDisplayMetrics());
		changeLocalModul();
	}

	public void changeEn() {
		String languageToLoad = "";
		Locale locale = new Locale(languageToLoad);
		Log.d("Locale", String.valueOf(Locale.getDefault()));
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config,
				getBaseContext().getResources().getDisplayMetrics());
		changeLocalModul();
	}

	public class SampleAuthListener implements AuthListener {

		public void onAuthSucceed() {
			Log.d("onAuthSucceed", "SampleAuthListener");
			mAsyncRunner.request("me", new SampleRequestListener());
		}

		public void onAuthFail(String error) {
			Log.d("onAuthFail", "SampleAuthListener");
		}
	}

	public class SampleLogoutListener implements LogoutListener {
		public void onLogoutBegin() {
			Log.d("SampleLogoutListener", "Logging out...");

			mAsyncRunner.request("me", new SampleRequestListener());
		}

		public void onLogoutFinish() {
			Log.d("SampleLogoutListener", "You have logged out! ");
		}
	}

	public class SampleRequestListener extends BaseRequestListener {

		public void onComplete(final String response, final Object state) {
			try {
				// process the response here: executed in background thread
				Log.d("Facebook-Example", "Response: " + response.toString());
				JSONObject json = Util.parseJson(response);
				final String name = json.getString("name");

				// Begin.this.runOnUiThread(new Runnable() {
				// public void run() {
				// Intent intent = new Intent(Begin.this,
				// RegisterActivity.class);
				// intent.putExtra("json", response.toString());
				// startActivity(intent);
				// overridePendingTransition(R.anim.in_from_bottom,
				// R.anim.out_to_top);
				// }
				// });
			} catch (JSONException e) {
				Log.w("Facebook-Example", "JSON Error in response");
			} catch (FacebookError e) {
				Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
			}
		}
	}
}
